import { getCodeScanSet, saveCodeScanSet } from '../../../../../../../../api/code'
import { ref } from "vue";
import { CodeScanSet } from "../types";
import _ from 'lodash'

export const useCodeScanSetService = () => {
	const getLoading = ref(false)
	const saveLoading = ref(false)

	const codeScanSets = ref<Array<CodeScanSet>>([])

	const defaultSets = ref<Array<CodeScanSet>>([])

	const get = async () => {
		getLoading.value = true
		const res = await getCodeScanSet()
		codeScanSets.value = res.data.data.row
		defaultSets.value = _.cloneDeep(codeScanSets.value)
		getLoading.value = false
	}

	const save = async () => {
		saveLoading.value = true
		const res = await saveCodeScanSet(codeScanSets.value)
		saveLoading.value = false
		return res.data
	}

	// 恢复为默认值
	const onDefault = () => {
		codeScanSets.value = _.cloneDeep(defaultSets.value)
	}

	return {
		getLoading,
		saveLoading,
		codeScanSets,
		get,
		save,
		onDefault
	}
}